package com.hotau.product;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;

import com.hotau.Executable;
import com.hotau.HotauActivity;
import com.hotau.HotauConstants;
import com.hotau.R;
import com.hotau.location.CommonData;
import com.hotau.member.service.MemberService;
import com.hotau.product.vo.Product;
import com.hotau.util.CalendarUtil;
import com.hotau.util.DialogUtil;
import com.hotau.util.HttpRequestAsynTask;
import com.hotau.util.HttpUtil;
import com.hotau.util.JsonParserUtil;

public class AddCommentFragment extends Fragment {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Product product;
	private EditText contentView;
	private RatingBar ratingBar;
	private ProductDetailFragment productDetailFragment;

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.product_comment_add, container, false);
	}

	@Override
	public void onActivityCreated(Bundle bundle) {
		Bundle dataBundle = getArguments();
		if (dataBundle != null) {
			this.product = (Product) dataBundle.getSerializable("product");
			this.productDetailFragment = (ProductDetailFragment) dataBundle.getSerializable("productDetailFragment");
		}

		super.onActivityCreated(bundle);
		setHasOptionsMenu(true);

		TextView commentTitleView = (TextView) this.getActivity().findViewById(R.id.product_comment_add_title);
		commentTitleView.setText(Html.fromHtml("对<font color='#ff6600'>\"" + product.getProductName() + "\"</font>发表点评"));

		TextView tv = (TextView) this.getActivity().findViewById(R.id.product_comment_add_estimate);
		tv.setText(Html.fromHtml("<font color='#ff0000'>*</font> "
				+ this.getActivity().getResources().getString(R.string.location_comment_add_estimate)));

		tv = (TextView) this.getActivity().findViewById(R.id.product_comment_add_description_hint);
		tv.setText(Html.fromHtml("<font color='#ff0000'>*</font> "
				+ this.getActivity().getResources().getString(R.string.location_comment_add_description)));

		contentView = (EditText) this.getActivity().findViewById(R.id.product_comment_add_description);
		ratingBar = (RatingBar) this.getActivity().findViewById(R.id.product_comment_add_rate);

		((Button) this.getActivity().findViewById(R.id.product_comment_add_submibt))
				.setOnClickListener(new OnClickListener() {
					@Override
					public void onClick(View v) {
						preSubmit();
					}
				});
	}

	private String checkInput() {
		String content = contentView.getText().toString().trim();
		if (content.length() < 20) {
			return "内容不能少于20字";
		}

		if (ratingBar.getRating() == 0) {
			return "请选择星级";
		}

		return null;
	}

	private void preSubmit() {
		String errorMessage = checkInput();
		if (errorMessage != null) {
			DialogUtil.showDialog(getActivity(), "错误", errorMessage);
		} else {
			submitData();
		}
	}

	private void submitData() {
		final HotauActivity ha = ((HotauActivity) getActivity());
		String url = HotauConstants.LOCATION_ADD_COMMENT;
		HttpRequestAsynTask task = new HttpRequestAsynTask(getActivity()) {
			@Override
			protected void doPostExecute(String result) {
				CommonData commonData = (CommonData) JsonParserUtil.str2Object(
						result, CommonData.class);
				if (commonData != null && commonData.errorMsg == null) {
					DialogUtil.showDialog(getActivity(), "提示 ", "成功添加评论!",
							new Executable() {
								@Override
								public void execute() {
									productDetailFragment
											.setNeedUpdateComment(true);
									if (ha != null) {
										ha.popFragment();
									}

								}
							});
				} else {
					if (commonData != null) {
						DialogUtil.showDialog(getActivity(), "提示",
								commonData.errorMsg);
					} else {
						DialogUtil.showDialog(getActivity(), "提示", "网络错误！");
					}
				}
			}
		};

		String actualRequest = "Form:productId=" + product.getProductId()
				+ ";userId=" + getUserId() + ";commentDate="
				+ CalendarUtil.getTodayyyyyMMddHHmmssStr() + ";commentContent="
				+ contentView.getText().toString().trim() + ";comment1Count="
				+ (int) ratingBar.getRating() + ";terminalCode="
				+ HotauConstants.getTerminalCode(getActivity());

		Log.d("mytag", "actualrequest =" + actualRequest);
		// 发起http异步请求－3：真正发起请求
		task.execute(url, actualRequest, HttpUtil.UTF8_ENCODING);
	}

	private String getUserId() {
		try {
			JSONObject userJson = MemberService.getUserInfo(getActivity());

			if (userJson != null) {
				return userJson.getString("accName");
			}

			return "13612345678";
		} catch (JSONException e) {
			e.printStackTrace();
			return "13612345678";
		}
	}
}
